package indi.h.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;


public class InterFaceProxy<T> implements InvocationHandler {
	private T target;
	private Map<Method, ProxyFilter<T>> proxyFilter;
	
	public InterFaceProxy(T target, Map<Method, ProxyFilter<T>> proxyFilter) {
		super();
		this.target = target;
		this.proxyFilter = proxyFilter;
	}

	public T getTarget() {
		return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
				this.target.getClass().getInterfaces(), this);
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		// TODO Auto-generated method stub
		method.getName();
		method.getReturnType();
		method.getParameterTypes();
		if (proxyFilter.get(method) != null) {
			ProxyFilter<T> filter = proxyFilter.get(method);
			filter.before();
			Object invoke = method.invoke(this.target, args);
			filter.after();
			return invoke;
		}
		return method.invoke(this.target, args);
	}
	
}
